package com.kede.pay.config;

import com.kede.pojo.ResHttp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/***
 ** @Author:Pr.li
 ** @Description: 全局异常处理
 ** @Date: create in 2023/4/4 17:33
 ***/
@Slf4j
@ControllerAdvice
public class CustomExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResHttp exceptionHandler(Exception e){
        log.debug("exceptionHandler",e.getCause());
        return ResHttp.failMsg(e.getMessage());
    }

    /***
     * @Description: 参数验证失败异常处理
     * @Author: Pr.li
     * @Date: 2023/4/4 17:38
     * @Param: [e]
     * @Return: com.kede.shangmenla.pojo.ResHttp
     ***/
    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResHttp ArgmentExcetion(MethodArgumentNotValidException e){
        log.debug("MethodArgumentNotValidException",e.getCause());
        StringBuilder msg = new StringBuilder("参数验证错误:");
        e.getBindingResult().getFieldErrors().forEach(fieldError -> {
            msg.append(fieldError.getDefaultMessage());
            msg.append(";");
        });
        return ResHttp.failMsg(msg.toString());
    }

}
